<?php
if(!defined('PP_API') || !defined('PP_NAME') || !defined('PP_KEY')) exit('Invalid Request');

eval('class passport extends pp_'.PP_NAME.'
{
	var $db;
	function passport()
	{
		$this->db = $GLOBALS["dsql"];
	}	
}');

/*phpwind */
class pp_phpwind
{
	var $pp_api;
	var $action;
	var $encode;
	var $forward;
	var $verify;

	function __construct()
	{
		!strstr(PP_API,'passport_client.php') && exit('Bad Request');
	}

	function pp_phpwind()
	{
		$this->__construct();
	}
	
	function login($userId,$pwd,$type=1,$gourl='')
	{
		global $forward,$gourl;
		$this->action = 'login';
		if(isset($forward))
		{
			$this->forward = rawurldecode($forward);
		}
		elseif(isset($gourl))
		{
			$gourl = str_replace("../","/",$gourl);
			$gourl = str_replace("//","/",$gourl);
			$this->forward = $GLOBALS["cfg_basehost"]."/".$gourl;
		}
		else
		{
			$this->forward = $GLOBALS["cfg_basehost"]."/plus/list.php";
		}
		$userdb = array();
		$rs = $this->db->GetOne("SELECT mid,uname,email,scores FROM #@__member WHERE userid='$userId'");
		$userdb['uid']		= $rs['mid'];
		$userdb['username']	= $userId;
		$userdb['password']	= md5($pwd);
		$userdb['email']	= $rs['email'];
		$userdb['credit']	= $rs['scores'];
		$userdb['time']		= time();
		$userdb['cktime']	= $userdb['time']+36000;
		$encode = '';
		foreach($userdb as $key => $val)
		{
			$encode .= $encode ? "&$key=$val" : "$key=$val";
		}
		$this->encode = str_replace('=', '', $this->StrCode($encode));
		$this->verify = md5("login".$this->encode.$this->forward.PP_KEY);
		$this->_interFace();
	}

	function logout()
	{
		global $forward;
		$this->forward = isset($forward) ? rawurldecode($forward) : $GLOBALS["cfg_basehost"];
		$userdb = array();
		$this->action = 'quit';
		$userdb['uid']		= '';
		$userdb['username']	= 'exit';
		$userdb['password']	= 'exit';
		$userdb['email']	= 'exit';
		$userdb['time']		= time();
		$userdb['cktime']	= $userdb['time']+36000;
		$encode = '';
		foreach($userdb as $key=>$val)
		{
			$encode .= $encode ? "&$key=$val" : "$key=$val";
		}
		$this->encode = str_replace('=', '', $this->StrCode($encode));
		$this->verify = md5("quit".$this->encode.$this->forward.PP_KEY);
		$this->_interFace();
	}

	function StrCode($string,$action='ENCODE')
	{
		$key	= substr(md5($_SERVER["HTTP_USER_AGENT"].PP_KEY),8,18);
		$string	= $action == 'ENCODE' ? $string : base64_decode($string);
		$len	= strlen($key);
		$code	= '';
		for($i=0; $i < strlen($string); $i++)
		{
			$k		= $i % $len;
			$code  .= $string[$i] ^ $key[$k];
		}
		$code = $action == 'DECODE' ? $code : base64_encode($code);
		return $code;
	}

	function _interFace()
	{
		$this->pp_api = PP_API."?action=".$this->action;
		$this->pp_api .= "&userdb=".rawurlencode($this->encode)."&forward=".rawurlencode($this->forward)."&verify=".rawurlencode($this->verify);
		reMessage($this->pp_api);
	}
}

/*discuz */

class pp_discuz
{
	var $pp_api;
	var $action;
	var $forward;
	var $auth;
	var $verify;

	function __construct()
	{
		!strstr(PP_API,'api/passport.php') && exit('Bad Request');
	}

	function pp_discuz()
	{
		$this->__construct();
	}

	function login($userId,$pwd,$type=1,$gourl='')
	{
		global $forward,$gourl;
		$this->action = "login";
		$rs = $this->db->GetOne("SELECT `uname`,`email`,`scores` FROM `#@__member` WHERE `userid`='$userId'");
		if(is_array($rs))
		{
			$userdb = array('cookietime'	=> time()+86400,'time' => time(),'credits' => $rs['scores'],'username'	=> $userId,	'password'	=> $pwd,'email' => $rs['email'],'regip'	=> GetIP(),	'regdate'	=> time(), 'nickname' => $rs['uname'],'msn' => $rs['email']);
		}
		else
		{
			return false;
		}
		if(!empty($forward))
		{
			$this->forward = rawurldecode($forward);
		}
		elseif(!empty($gourl))
		{
			$gourl = str_replace("../","/",$gourl);
			$gourl = str_replace("//","/",$gourl);
			$this->forward = $GLOBALS["cfg_basehost"]."/".$gourl;
		}
		else
		{
			$this->forward = $GLOBALS["cfg_basehost"]."/plus/list.php";
		}
		$this->auth = $this->enCrypt($this->enCode($userdb), PP_KEY);
		$this->verify =  md5($this->action.$this->auth.$this->forward.PP_KEY);
		$this->_interFace();
	}

	function logout()
	{
		global $forward;
		$this->action = "logout";
		$this->forward = isset($forward) ? rawurldecode($forward) : $GLOBALS["cfg_basehost"];
		$userdb = array('cookietime' => -86400*time(),'time' => time(),'username'	=> 'exit','password' => 'exit','email' => 'exit@exit.com',);
		$this->auth = $this->enCrypt($this->enCode($userdb), $GLOBALS["cfg_cookie_encode"]);
		$this->verify =  md5($this->action.$this->auth.$this->forward.PP_KEY);
		$this->_interFace();
	}

	function _interFace()
	{
		reMessage(PP_API."?action=".$this->action."&auth=".rawurlencode($this->auth)."&forward=".rawurlencode($this->forward)."&verify=".$this->verify);
	}

	function enCrypt($txt, $key)
	{
		srand((double)microtime() * 1000000);
		$encrypt_key = md5(rand(0, 32000));
		$ctr = 0;
		$tmp = '';
		for($i = 0; $i < strlen($txt); $i++)
		{
			$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
			$tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]);
		}
		return base64_encode($this->setKey($tmp, $key));
	}

	function deCrypt($txt, $key)
	{
		$txt = $this->setKey(base64_decode($txt), $key);
		$tmp = '';
		for ($i = 0; $i < strlen($txt); $i++)
		{
			$tmp .= $txt[$i] ^ $txt[++$i];
		}
		return $tmp;
	}

	function setKey($txt, $encrypt_key)
	{
		$encrypt_key = md5($encrypt_key);
		$ctr = 0;
		$tmp = '';
		for($i = 0; $i < strlen($txt); $i++)
		{
			$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
			$tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
		}
		return $tmp;
	}

	function enCode($array)
	{
		$arrayenc = array();
		foreach($array as $key => $val) $arrayenc[] = $key.'='.urlencode($val);
		return implode('&', $arrayenc);
	}
}

function reMessage($url)
{
	echo '
	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title></title>
	<style type="text/css">
	body{margin:0;width:100%;height:100%} body,td,input,textarea,select{font-family:arial,sans-serif} input,textarea,select{font-size:100%} #loading{position:absolute;width:100%;height:100%;z-index:1000;background-color:#fff} .cmsg{margin:1em} .msg{font-weight:bold} .lpb{margin:2px 0;border:1px solid #949dad;width:300px;height:0.5em;overflow:hidden;padding:1px} #lpt{background:#d4e4ff;width:0;height:100%;font-size:0} .invfr{position:absolute;left:0;top:0;z-index:-1;width:0;height:0;border:0} .msgb{position:absolute;right:0;bottom:10px;font-size:12px;font-weight:normal;color:#000;background:#fff;padding:20px}
	</style>
	</head>
	<body>
	<script type="text/javascript" reload="1">
	var pgo=1;function JumpUrl(){ ++pgo; document.getElementById("lpt").style.width = parseInt(pgo*100)+"px"; if(pgo==3){ location="'.$url.'";clearInterval(timer);}}
	timer = setInterval("JumpUrl()",1000);
	</script>
	<div id="loading">
	<div class="cmsg">
	<div class="msg"></div>
	<div class="lpb"> <div id="lpt"></div> </div> </div>
	</div>
	</body>
	</html>';exit;
}
?>

